package com.linln.admin.system.web;

import com.github.pagehelper.Page;
import com.linln.admin.system.validator.UserAddressValid;
import com.linln.common.enums.StatusEnum;
import com.linln.common.utils.HttpServletUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.utils.StatusUtil;
import com.linln.common.vo.ResultVo;
import com.linln.component.actionLog.annotation.EntityParam;
import com.linln.modules.system.domain.User;
import com.linln.modules.system.entity.UserAddress;
import com.linln.modules.system.entity.UserBill;
import com.linln.modules.system.service.AddressServices;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping(value = "/mains/address")
public class AddressController extends BaseController {
    @Autowired
    private AddressServices addressServices;

    @RequestMapping(value = "/index")
    @RequiresPermissions("mains:address:index")
    public ModelAndView getIndex(Integer pageNum) {
        Integer pageIndex = HttpServletUtil.getParameterInt("size", 1);
        pageNum = pageNum == null ? super.pageNum : pageNum;
        ModelAndView view = new ModelAndView();
        view.setViewName("/mains/address/index");
        Page<UserAddress> userAddress = addressServices.getUserAddress(pageNum, pageIndex*10);
        view.addObject("list", userAddress.getResult());
        view.addObject("page", userAddress);
        return view;
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    @RequiresPermissions("mains:address:add")
    public String toAdd() {
        return "/mains/address/add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("mains:address:edit")
    public String toEdit(@PathVariable("id") String id, Model model) {
        UserAddress userAddress = addressServices.selectById(id);
        model.addAttribute("userAddress", userAddress);
        return "/mains/address/add";
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("mains:address:detail")
    public String toDetail(@PathVariable("id") String id, Model model) {
        UserAddress userAddress = addressServices.selectById(id);
        model.addAttribute("userAddress", userAddress);
        return "/mains/address/detail";
    }

    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     * @param userAddress 实体对象
     */
    @PostMapping("/save")
    @RequiresPermissions({"mains:address:add", "mains:address:edit"})
    @ResponseBody
    public ResultVo save(@Validated UserAddressValid valid, @EntityParam UserAddress userAddress) {
        // 保存数据
        addressServices.saveAddress(userAddress);
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 设置一条或者多条数据的状态
     */
    @RequestMapping("/status/{param}")
    @RequiresPermissions("mains:userAddress:status")
    @ResponseBody
    public ResultVo updateStatus(
            @PathVariable("param") String param,
            @RequestParam(value = "ids", required = false) List<String> ids) {

        // 更新状态
        StatusEnum statusEnum = StatusUtil.getStatusEnum(param);
        addressServices.updateStatus(statusEnum,ids);
        return ResultVoUtil.DELETE_SUCCESS;
    }
}
